package com.boyuan.huijiaoyu;

import java.io.Serializable;
import java.util.ArrayList;

import com.example.contactfragment.ContactService;
import com.example.newfragment.DetailPerson;
import com.magicsoft.app.helper.SharePreferenceHelper;
import com.magicsoft.app.helper.ToastHelper;
import com.magicsoft.app.wcf.listener.GetOneRecordListener;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class ContactDeailSettingActivity extends Activity implements OnClickListener{

	private TextView tv1;
	private TextView tv2;
	private String isxingbiao;
	private DetailPerson homeContactDetailEntity;
	private ContactService contactService;
	private String yonghuID;
	private String uid;
	boolean isrequestok = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contact_deail_setting);
		SharedPreferences sp = getSharedPreferences("a.xml", 0);
		yonghuID = sp.getString("id", "");
		Intent intent = getIntent();
		uid = intent.getStringExtra("uid");
		getContactDetail(yonghuID);
//		homeContactDetailEntity = (DetailPerson) intent.getSerializableExtra("homeContactDetailEntity");
//		isxingbiao = homeContactDetailEntity.getIs_favorite();
		gengxingUI();	
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		finish();
		return super.onTouchEvent(event);
	}

	private void gengxingUI() {
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				
				if(isrequestok){
					initUI();
					return;
				}else{
					new Handler().postDelayed(this, 100);
				}
			}
		}, 100);
	}
	
	private void getContactDetail(String contactsID) {


		if (contactService == null) {
			contactService = new ContactService(getApplicationContext());
		}

		contactService.getContactdetail(yonghuID, uid,
				new GetOneRecordListener<DetailPerson>() {

					@Override
					public void onFinish(DetailPerson data) {
						homeContactDetailEntity = data;
						isrequestok = true;
					}

					@Override
					public void onFailed(String Message) {
					}
				});
	}

	private void initUI() {
		isxingbiao = homeContactDetailEntity.getIs_favorite();
		tv1 = (TextView) findViewById(R.id.textView1);
		tv2 = (TextView) findViewById(R.id.textView2);
		if(isxingbiao.equals("1")){
			tv1.setText("取消星标");
		}else{
			tv1.setText("添加星标");
		}
		findViewById(R.id.lin_email).setOnClickListener(this);
		findViewById(R.id.lin_shenpi).setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.contact_deail_setting, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lin_email:
			if(isxingbiao.equals("1")){
				deleteContactFavorite();
				finish();
			}else{
				putContactFavorite();
				finish();
			}
			break;
		case R.id.lin_shenpi:
			deleteFrequentContacts();
			finish();
			break;
		default:
			break;
		}
	}

	/**
	 * 添加星标
	 */
	private void putContactFavorite() {

		
		String contactsID = homeContactDetailEntity.getUid();

		if (contactService == null) {
			contactService = new ContactService(getApplicationContext());
		}

		contactService.putContactFavorite(yonghuID, contactsID,
				new GetOneRecordListener<String>() {

					@Override
					public void onFinish(String data) {
						homeContactDetailEntity.setIs_favorite("1");
						Intent intent = new Intent();
						intent.setAction("AddXingbiao");
						sendBroadcast(intent);
						ToastHelper.showMsg(ContactDeailSettingActivity.this,"添加成功", false);
					}

					@Override
					public void onFailed(String Message) {
						ToastHelper.showMsg(ContactDeailSettingActivity.this,
								Message, false);
					}
				});
	}

	/**
	 * 取消星标
	 */
	private void deleteContactFavorite() {

		String contactsID = homeContactDetailEntity.getUid();

		if (contactService == null) {
			contactService = new ContactService(getApplicationContext());
		}

		contactService.deleteContactFavorite(yonghuID, contactsID,
				new GetOneRecordListener<String>() {

					@Override
					public void onFinish(String data) {
						homeContactDetailEntity.setIs_favorite("2");
						Intent intent = new Intent();
						intent.setAction("ShanchuXingbiao");
						sendBroadcast(intent);
						ToastHelper.showMsg(ContactDeailSettingActivity.this,"取消成功", false);
					}

					@Override
					public void onFailed(String Message) {
						ToastHelper.showMsg(ContactDeailSettingActivity.this,
								Message, false);
					}
				});
	}
	
	/**
	 * 删除好友
	 */
	private void deleteFrequentContacts() {

		if (contactService == null) {
			contactService = new ContactService(this);
		}
		String contactsID = homeContactDetailEntity.getUid();

		contactService.deleteFrequentContact(yonghuID, contactsID,
				new GetOneRecordListener<String>() {

			@Override
			public void onFinish(String data) {
				Intent intent = new Intent();
				intent.setAction("ShanchuChangyong");
				sendBroadcast(intent);
				Intent intent2 = new Intent();
				intent2.setAction("CloseContactDetail");
				sendBroadcast(intent2);
				ToastHelper.showMsg(ContactDeailSettingActivity.this, data, false);
			}

			@Override
			public void onFailed(String Message) {
				ToastHelper.showMsg(ContactDeailSettingActivity.this, Message, false);
			}
		});
	}


}
